
import 'package:flutter_getx/core/utils/log_utils.dart';
import 'package:get/get.dart';

import 'delegate/dio_delegate.dart';
import 'delegate/event_delegate.dart';
import 'delegate/i_controller_delegate.dart';

class BaseController extends GetxController {

  final Map<String, IControllerDelegate> _delegateMap = {};

  IC? getDelegate<IC>(){
    return _delegateMap['$IC'] as IC?;
  }

  void addDelegate(IControllerDelegate ic){
    _delegateMap['${ic.runtimeType}'] = ic;
  }

  DioDelegate get dio => getDelegate<DioDelegate>()!;
  EventDelegate get event => getDelegate<EventDelegate>()!;

  @override
  void onInit() {
    lprint('$this ==> onInit');
    addDelegate(EventDelegate());
    addDelegate(DioDelegate());
    _delegateMap.forEach((_, value) {value.onInit();});
    super.onInit();
  }

  @override
  void onReady() {
    lprint('$this ==> onReady');
    _delegateMap.forEach((_, value) {value.onReady();});
    super.onReady();
  }

  @override
  void onClose() {
    lprint('$this ==> onClose');
    _delegateMap.forEach((_, value) {value.onClose();});
    super.onClose();
  }

}